from django.shortcuts import render
from django.http import HttpResponse
from apiV1.models import Card,Comment,Deck,LearnPlan
import apiV1.dbop as dbop
import json
from apiV1.dbop import *
# Create your views here.
from django.views.decorators.csrf import csrf_exempt

def resUTF8(res):
    return HttpResponse(res, content_type='application/json', charset='utf-8')

def index(request,op,obj):
    methodname = 'ut_' + op + obj
    if op == 'add':
        data = request.POST
        result = eval(methodname)(data)
    elif op == 'remove':
        data = request.GET.get(obj.lower()+'id')
        result = eval(methodname)(data)
    elif op == 'get':
        data = request.GET
        result = eval(methodname)(data)
    elif op == 'modify':
        data = request.POST
        result = eval(methodname)(data)
    else:
        res = json.dumps({
            'success': 'no',
            'result': 'wrong op!',
        }, ensure_ascii=False)
        return resUTF8(res)
    res = json.dumps({
        'success':'ok',
        'result':result,
    }, ensure_ascii=False)
    return resUTF8(res)
